package com.lechuang.yizhezhe.view.activity.own;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.ScrollView;
import android.widget.TextView;

import com.lechuang.yizhezhe.model.bean.MyIncomeBean;
import com.lechuang.yizhezhe.presenter.CommonAdapter;
import com.lechuang.yizhezhe.presenter.net.netApi.CommenApi;
import com.lechuang.yizhezhe.R;
import com.lechuang.yizhezhe.presenter.net.ResultBack;
import com.lechuang.yizhezhe.presenter.net.Netword;
import com.lechuang.yizhezhe.view.defineView.MListView;
import com.lechuang.yizhezhe.view.defineView.ViewHolder;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;


/**
 * 作者：li on 2017/10/6 14:37
 * 邮箱：961567115@qq.com
 * 修改备注:
 */
public class MyIncomeActivity extends AppCompatActivity {
    @BindView(R.id.tv_Integral)
    TextView tvIntegral;
    @BindView(R.id.tv_estimated)
    TextView tvEstimated;
    @BindView(R.id.tv_totals)
    TextView tvTotals;
    @BindView(R.id.tv_reward)
    TextView tvReward;
    @BindView(R.id.tv_grossed)
    TextView tvGrossed;
    @BindView(R.id.tv_cash)
    TextView tvCash;      //提现
    @BindView(R.id.tv_detail)
    TextView tvDetail;    //明细
    @BindView(R.id.mlv_income)
    MListView mlvIncome;
    @BindView(R.id.sv)
    ScrollView sv;
    @BindView(R.id.iv_back_income)
    ImageView ivBackIncome;

    private Context mContext = MyIncomeActivity.this;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_income);
        ButterKnife.bind(this);
        initVeiw();
    }

    private void initVeiw() {
        //scrollview从最上面出现
        // ((ScrollView) findViewById(R.id.sv)).smoothScrollTo(0, 0);
        //获取焦点  必须先获取焦点才能在顶部  另外内部的listview gridView不能有焦点
        sv.setFocusable(true);
        sv.setFocusableInTouchMode(true);
        sv.requestFocus();
        sv.smoothScrollTo(0, 0);
    }

    @Override
    protected void onStart() {
        super.onStart();
        //onstart里面执行 提现积分回来后 可刷新积分信息
        initData();
    }

    public void initData() {
        Netword.getInstance().getApi(CommenApi.class)
                //这里不传page
                .myIncome("")
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ResultBack<MyIncomeBean>(this) {
                    @Override
                    public void successed(MyIncomeBean result) {
                        if (result == null)
                            return;
                        MyIncomeBean.RecordBean recordBean = result.record;
                        //积分收益列表数据
                        List<MyIncomeBean.RecordBean.ListBean> list = result.record.list;
                        //可提现积分
                        tvIntegral.setText(recordBean.withdrawIntegral);
                        //总预估收入
                        tvEstimated.setText(recordBean.estimatedIncome);
                        //总结算收入
                        tvTotals.setText(recordBean.totalIncome);
                        //当月总结算收入
                        tvGrossed.setText(recordBean.currentMonthIncome);
                        //奖励收入
                        tvReward.setText(recordBean.rewardIncome);
                        mlvIncome.setAdapter(new CommonAdapter<MyIncomeBean.RecordBean.ListBean>(mContext, list, R.layout.item_myincome) {
                            @Override
                            public void setData(ViewHolder viewHolder, Object item) {
                                MyIncomeBean.RecordBean.ListBean bean = (MyIncomeBean.RecordBean.ListBean) item;
                                //收益中文描述
                                viewHolder.setText(R.id.tv_income, bean.typeStr);
                                //时间
                                viewHolder.setText(R.id.tv_time, bean.createTimeStr);
                                //积分明细
                                //积分明细
                                Double integralDetails = Double.parseDouble(bean.integralDetailsStr);
                                if (integralDetails > 0) {  //正积分 加上正号
                                    viewHolder.setText(R.id.tv_num, "+" + bean.integralDetailsStr);
                                } else {       //负积分  不处理
                                    viewHolder.setText(R.id.tv_num, bean.integralDetailsStr);
                                }
                            }
                        });
                    }
                });
    }

    @OnClick({R.id.tv_cash, R.id.tv_detail, R.id.iv_back_income})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_cash:  //积分提现
                startActivity(new Intent(this, JinfenReflectActivity.class));
                break;
            case R.id.tv_detail://明细
                startActivity(new Intent(this, ProfitActivity.class));
                break;
            case R.id.iv_back_income:
                finish();
        }
    }


}
